package com.zixun.controller;

import com.zixun.config.WebApiConfig;
import com.zixun.model.IndexImage;
import com.zixun.model.information.Information;
import com.zixun.model.User;
import com.zixun.repository.IndexImageRepository;
import com.zixun.repository.information.InformationRepository;
import com.zixun.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Transactional
@RequestMapping("/api/index/")
public class IndexController {

    @Autowired
    UserRepository userRepository;
    @Autowired
    InformationRepository informationRepository;
    @Autowired
    IndexImageRepository indexImageRepository;


    /**
     * @api {GET} /api/index/info 获取首页信息
     * @apiName info
     * @apiGroup index
     * @apiVersion 0.0.1
     * @apiDescription 获取首页信息
     * @apiParam {String} userId 用户编号
     * @apiParam {String} city 城市名称
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功,2:参数错误,3:服务器异常】,
     * "errMsg":【错误信息,字符串】,
     * "data":
     * {
     *  "users":[{
     *      "name":【行家名称，字符串】,
     *      "introduce":【行家简介,字符串】,
     *      "id":【行家编号,字符串】,
     *      "headPicture":【行家头像，字符串】
     *  }],
     *  "images":[{
     *      "url":【轮播图，字符串】,
     *      "id":【编号，整形】
     *  }],
     *  "hotInfo":[{
     *      "title":【热门信息标题,字符串】,
     *      "introduce":【热门信息简介，字符串】,
     *      "price":【价格，整形】,
     *      "id":【编号，整形】
     *  }]
     * }
     * }
     */
    @RequestMapping("info")
    public Map index(String userId,String city){
        Map result = new HashMap();
        if(userId==null||city==null){
            result.put("returnValue",2);
            result.put("errMsg","参数错误");
            return result;
        }
        Page<User> users = userRepository.findAll(new PageRequest(0,3));
        List listUser = new ArrayList();
        for(User user : users){
            Map map = new HashMap();
            map.put("headPicture", WebApiConfig.IMAGE_URL+user.getHeadPicture());
            map.put("name",user.getName());
            map.put("id",user.getId().toString());
            map.put("introduce",user.getIntroduce());
            listUser.add(map);
        }
        List<IndexImage> indexImageList = indexImageRepository.findAll();
        List<Information> listInformation = informationRepository.findAll(new Sort(Sort.Direction.DESC,"hot"));
        List images = new ArrayList();
        for(IndexImage image: indexImageList){
            Map map = new HashMap();
            map.put("url",image.getUrl());
            map.put("id",image.getId());
            images.add(map);
        }
        List list = new ArrayList();
        for(Information information:listInformation){
            Map map = new HashMap();
            map.put("title",information.getTitle());
            map.put("introduce",information.getIntroduce());
            map.put("price",information.getPrice());
            map.put("id",information.getId());
            list.add(map);
        }
        Map map = new HashMap();
        map.put("users",listUser);
        map.put("images",indexImageList);
        map.put("hotInfo",list);
        result.put("returnValue",1);
        result.put("errMsg",null);
        result.put("data",map);
        return result;
    }

}
